from PySide6.QtWidgets import QDialog,QMessageBox
from sqlalchemy.orm import Session
from settings.publicvar import engine

from views.bankinfo.bankinfo.operation import Ui_operation
from views.bankinfo.model.bankinfo import BankInfo


class OperationBankInfoView(QDialog,Ui_operation):
    def __init__(self,is_state,data):
        super().__init__()
        self.setupUi(self)
        self.name_id = False
        self.if_status(is_state)
        self.setFormStructure(data)
        self.savebtn.clicked.connect(self.saveFun)
        self.changebtn.clicked.connect(self.changeFun)
        self.deletebtn.clicked.connect(self.deleteFun)
        self.resetbtn.clicked.connect(self.resetFun)

    # 状态判断
    def if_status(self,is_status):
        if is_status == 'add':
            self.deletebtn.hide()
            self.changebtn.hide()
        elif is_status == 'change':
            self.titlelabel.setText('修改银行卡信息')
            self.savebtn.hide()
            self.deletebtn.hide()
        elif is_status == 'delete':
            self.titlelabel.setText('删除银行卡信息')
            self.savebtn.hide()
            self.changebtn.hide()
            self.resetbtn.hide()
        else:
            self.titlelabel.setText('查看银行卡信息')
            self.savebtn.hide()
            self.changebtn.hide()
            self.resetbtn.hide()
            self.deletebtn.hide()

    # 表单加载
    def setFormStructure(self,data):
        if data:
            self.idx.setText(data[0])
            self.bankname.setText(data[1])
            self.name.setText(data[2])
            self.IDNumber.setText(data[3])
            self.cardnumber.setText(data[4])
            self.openbank.setText(data[5])
            self.pupose.setText(data[6])

    # 获取表单数据
    def getFormData(self):
        idx = self.idx.text().strip()
        bankname = self.bankname.text().strip()
        name = self.name.text().strip()
        IDNumber = self.IDNumber.text().strip()
        cardnumber = self.cardnumber.text().strip()
        openbank = self.openbank.text().strip()
        pupose = self.pupose.text().strip()

        data = {'bankname':bankname,'name':name,'IDNumber':IDNumber,'cardnumber':cardnumber,
                'openbank':openbank,'pupose':pupose}
        if idx is None:
            return [False,data]
        else:
            return [idx,data]

    # 保存数据
    def saveFun(self):
        data = self.getFormData()
        with Session(engine) as session,session.begin():
            bankcard = BankInfo(**data[1])
            session.add(bankcard)
            QMessageBox.information(None,'成功','恭喜添加成功！')
        self.close()

    def changeFun(self):
        data = self.getFormData()
        with Session(engine) as sessin,sessin.begin():
            sessin.query(BankInfo).filter(BankInfo.id == data[0]).update(data[1])
            QMessageBox.information(None, '成功', '恭喜修改成功！')
        self.close()
        print(data,'data')

    def deleteFun(self):
        data = self.getFormData()
        with Session(engine) as session,session.begin():
            with Session(engine) as sessin, sessin.begin():
                sessin.query(BankInfo).filter(BankInfo.id == data[0]).delete()
                QMessageBox.information(None, '成功', '恭喜删除成功！')
            self.close()

    def resetFun(self):
        self.bankname.setText('')
        self.name.setText('')
        self.IDNumber.setText('')
        self.cardnumber.setText('')
        self.openbank.setText('')
        self.pupose.setText('')
